<template>
	<view class="p40">
		<image src="/static/logo.png" mode="widthFix" class="logo"></image>
		<u-input v-model="mobile"  prefixIcon="phone"  placeholder="手机号" border="bottom"></u-input>
		<u-input v-model="captcha"  prefixIcon="lock"  placeholder="验证码" border="bottom">
			<template slot="suffix">
				<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
				<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
			</template>
		</u-input>
		<u-gap></u-gap>
		<u-button type="primary" shape="circle" @click="register">立即注册</u-button>
		<view class="mt20 pt20">
			<text>已有账号，去</text>
			<text style="color: #00aa00;" @click="go2login">登录</text>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				mobile: '',
				tips: '',
				captcha: ''
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			go2login(){
				uni.navigateTo({
					url:"/pages/auth/login"
				})
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
				
					//模拟获取验证码
					setTimeout(function(){
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					},2000)	

				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			},
			send() {
				this.api.sendSms({
					country: this.country,
					mobile: this.mobile
				}).then(res => {
					console.log(res)
				})
			},
			register() {
				this.api.register({
					mobile:this.mobile,
					captcha:this.captcha
				}).then(data => {
					uni.setStorageSync('userInfo', data.userinfo)
					uni.showToast({
						title: '注册成功'
					})

					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}, 1500)
				})
			}
		}
	}
</script>

<style scoped>
	.p40 {
		padding:0px  15%;
	}

	.logo {
		width: 100%;
		margin: 50upx auto;
	}
</style>